package pe.edu.upc.e41a.javasoft.jsf.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.converter.RooJsfConverter;
import pe.edu.upc.e41a.javasoft.domain.TipoDocumento;
import pe.edu.upc.e41a.javasoft.service.TipoDocumentoService;

@Configurable
@FacesConverter("pe.edu.upc.e41a.javasoft.jsf.converter.TipoDocumentoConverter")
@RooJsfConverter(entity = TipoDocumento.class)
public class TipoDocumentoConverter implements Converter {

	@Autowired
    TipoDocumentoService tipoDocumentoService;

	public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        Integer id = Integer.parseInt(value);
        return tipoDocumentoService.findTipoDocumento(id);
    }

	public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value instanceof TipoDocumento ? ((TipoDocumento) value).getCodigo().toString() : "";
    }
}
